#include <bits/stdc++.h>
using namespace std;
int n, m;
struct dd
{
    int x;
    int pos;
} b[500020];
inline bool cmp(dd x, dd y)
{
    return x.x == y.x ? x.pos > y.pos : x.x < y.x;
}
int a[500020], c[5000050], d[500020];
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d", &b[i].x);
        b[i].pos = i;
    }
    sort(b + 1, b + 1 + m, cmp);
    for (int i = 1; i <= n; ++i)
    {
        int pd = 0;
        for (int j = 1; j <= m; ++j)
        {
            if (a[i] == b[j].x)
            {
                c[++c[0]] = b[j].pos;
                pd = 1;
            }
            else if (pd)
                break;
        }
    }
    if (!c[0])
        return 0;
    d[++d[0]] = c[1];
    for (int i = 2; i <= c[0]; ++i)
    {
        if (c[i] > d[d[0]])
            d[++d[0]] = c[i];
        else
            *lower_bound(d + 1, d + 1 + d[0], c[i]) = c[i];
    }
    printf("%d", d[0]);
}